package tijava.generics;

import java.lang.reflect.Array;

public class GenericFoo<T extends Fruit> {
	private T[] ta;
	private Class<T> clz;

	private T x;

	public GenericFoo(T x) {
		this.x = x;

		// ERROR: Cannot instantiate the type T
		// this.x = new T(x);
		// this.x = new T();
	}

	public GenericFoo(Class<T> clz) {
		// Cannot create a generic array of T
		// this.ta = new T[3];

		// WARNING: Type safety: Unchecked cast from Object[] to T[]
		this.ta = (T[]) new Object[3];

		// best way to create generic array is use Class info
		// WARNING: Type safety: Unchecked cast from Object to T[]
		this.ta = (T[]) Array.newInstance(clz, 3);

	}

	public T getX() {
		return x;
	}

	public void setX(T x) {
		this.x = x;
	}
}
